#ifndef COMPRESSEDQIMAGE_H
#define COMPRESSEDQIMAGE_H

#include <QObject>
#include <QImage>
#include <QMap>
#include <QPair>
#include <QRgb>

class CompressedQImage
{
public:
    CompressedQImage(const QImage &q1, const QImage &q2)
    {
        if (q1.height() == q2.height() && q1.width() == q2.width())
        {
            QMap<QPair<int, int>, QRgb> tempvalue;
            for (int i = 0; i < q1.width(); ++i)
            {
                for (int j = 0; j < q1.height(); ++j)
                {
                    if (q1.pixel(i, j) != q2.pixel(i, j))
                    {
                        tempvalue.insert(QPair<int, int>(i, j), q2.pixel(i, j));
                    }
                }
            }
            QDataStream *stream = new QDataStream(&value, QIODevice::WriteOnly);
            (*stream) << tempvalue;
            delete stream;
        }
    }

    QByteArray getValue()
    {
        return value;
    }

    static QImage addTo(QByteArray &value, const QImage &old)
    {
        QMap<QPair<int, int>, QRgb> tempvalue;
        QDataStream *stream = new QDataStream(&value, QIODevice::ReadOnly);
        (*stream) >> tempvalue;

        QImage result(old.width(), old.height(), old.format());
        if (value.size() > 0)
        {
            for (int i = 0; i < old.width(); ++i)
            {
                for (int j = 0; j < old.height(); ++j)
                {
                    if (tempvalue.contains(QPair<int, int>(i, j)))
                    {
                        result.setPixel(i, j, tempvalue.value(QPair<int, int>(i, j)));
                    }
                    else
                    {
                        result.setPixel(i, j, old.pixel(i, j));
                    }
                }
            }
        }
        return result;
    }

private:
    QByteArray value;
};

#endif // COMPRESSEDQIMAGE_H
